package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 芦博
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2025 年 11 月 1 日
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    //Service
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;
    //Mapper
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    //业务层_自定义分页查询数据
    @Override
    public void customQueryPage(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        leaseAgreementMapper.customQueryPage(page, queryVo);

    }

    //根据id查询租约信息
    @Override
    public AgreementVo  cumstomGetById(Long id) {
        //获取lease继承基本信息表
        LeaseAgreement leaseAgreement = super.getById(id);
        Long apartmentId = leaseAgreement.getApartmentId();
        Long roomId = leaseAgreement.getRoomId();
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();

        //签约公寓信息_apartmentinfo
        LambdaQueryWrapper<ApartmentInfo> wrapper_apart = new LambdaQueryWrapper<>();
        wrapper_apart.eq(ApartmentInfo::getId, apartmentId);
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(wrapper_apart);
        //签约房间信息_roominfo
        LambdaQueryWrapper<RoomInfo> wrapper_room = new LambdaQueryWrapper<>();
        wrapper_room.eq(RoomInfo::getId, roomId);
        RoomInfo roomInfo = roomInfoService.getOne(wrapper_room);
        //支付类型_paymentType
        LambdaQueryWrapper<PaymentType> wrapper_pay = new LambdaQueryWrapper<>();
        wrapper_pay.eq(PaymentType::getId, paymentTypeId);
        PaymentType paymentType = paymentTypeService.getOne(wrapper_pay);
        //租约_leaseTerm
        LambdaQueryWrapper<LeaseTerm> wrapper_lease = new LambdaQueryWrapper<>();
        wrapper_lease.eq(LeaseTerm::getId, leaseTermId);
        LeaseTerm leaseTerm = leaseTermService.getOne(wrapper_lease);
        //保存信息
        AgreementVo vo = new AgreementVo();
        vo.setApartmentInfo(apartmentInfo);
        vo.setRoomInfo(roomInfo);
        vo.setPaymentType(paymentType);
        vo.setLeaseTerm(leaseTerm);
        BeanUtils.copyProperties(leaseAgreement,vo);
        return vo;
    }

}




